# SleepMirror - A reflection of your sleep quality
# 
# sm_eq135.rb
# TODO: Write out @answer to a file.
# This is a specific class for the EQ135 image.  I'm doing it this way due to time
# constraints.  I need to comeback and better abstract this functionality.  I'm thinking
# I need to put the make_bright [and make_dim?] methods into a Module.
# 
# ©2010 JohnGary, LLC.  All Rights Reserved.
# 
# 
# Changelog:
# 
# 
# 

require 'sm_panel'
require 'rubygame'

include Rubygame
include Rubygame::Events
include Rubygame::EventTriggers
include Rubygame::EventActions

DEFAULT_ASSET = "Eq135.png"
MAXALPHA = 255 # Maximum alpha value
ALPHASTEP = 1  # How much to dim/brighten the image
MINALPHA = 0   # Mimimum alpha value

class SM_eq135 < SM_panel
  attr_accessor :screen, :image

  # A new SM_visual_truefalse adds a Rubygame::Screen to the base SM_panel class.
  # A visual test is intended to blit(draw) it's asset, an image most likely, onto
  # Screen.
  def initialize(name="", instructions="", asset=DEFAULT_ASSET)
    super(name, instructions, asset)
    @screen=nil
    # Load the image
    this_dir = File.dirname( __FILE__ )
    file = File.expand_path( @asset, this_dir ) # default file
    @image = Surface.load( file ) #In this context, the asset is expected to be an image
    @image.colorkey=@image.get_at([0,0])
    @image.alpha=0 #start transparent, then make_bright until we get an keypress from user

    # Add the True and False hooks to the base class' event table
    is_false_hook = EventHook.new( :owner => nil,
      :trigger => KeyPressTrigger.new(:f),
      :action  => MethodAction.new(:capture_answer))
    is_true_hook = EventHook.new(  :owner => nil,
      :trigger => KeyPressTrigger.new(:t),
      :action  => MethodAction.new(:capture_answer))
    append_hook(is_false_hook) #Add the T/F hooks to the event table
    append_hook(is_true_hook) #Add the T/F hooks to the event table

  end
  def capture_answer(event)
    puts "Captured #{event.key}"
    @answer = event.key
    elapsed_time = Time.now - $t
    puts "Elapased time #{elapsed_time}s"
    TTF.setup()
    ttfont_path = File.join(File.dirname(__FILE__),"FreeSans.ttf")
    ttfont = TTF.new( ttfont_path, 24 )
    result = ttfont.render( "You chose #{event.key.upcase}; that is #{event.key==:f ? "Correct!" : "incorrect."} Your time was #{elapsed_time}s.  Press ESC to continue.", true, :white )
    result.blit( @screen,[@image.size[0]/2-result.size[0]/2,@image.size[1]/2])
  end
  def make_bright
    @screen=Screen.new(@image.size,0,[HWSURFACE])
    @screen.fill(@image.colorkey)
    @screen.show_cursor=FALSE
    @image.alpha > MAXALPHA-ALPHASTEP ? @image.alpha=MAXALPHA : @image.alpha += ALPHASTEP
    @image.blit(@screen,[0,0])

    TTF.setup()
    ttfont_path = File.join(File.dirname(__FILE__),"FreeSans.ttf")
    ttfont = TTF.new( ttfont_path, 30 )
    result = ttfont.render( "A simple equation will appear soon. Press T for True or F for False",true,:blue)
    result.blit( @screen,[@image.size[0]/2-result.size[0]/2,5])
    #puts "Brighten: Image alpha now #{$image.alpha}"
  end
  def perform
    # Setup the Screen for this test
    @screen = Screen.new(@image.size,0,[HWSURFACE])
    @screen.title = "#{@name} - #{@asset}" #File.basename(file)
    @screen.fill(@image.colorkey) #Fill the screen with the image's background
    @image.blit( @screen, [0,0] ) #Paste the asset onto the screen
    # Setup the event queue for this test
    events=EventQueue.new
    events.enable_new_style_events

    $t=Time.now #Capture the starting time.

    # Loop until we get an answer or an exit.
    loop do
      events = Rubygame::Events.fetch_sdl_events
      events << make_bright if @answer.nil?

      events.each do |event|
        handle( event )
      end
      @screen.update
      sleep 0.1
    end
  end
  def exit_script
    @answer = "Not Answered" if @answer.nil?
    puts "#{@name}- answer: #{@answer}"
    super
  end
end
